

if (Test-Path $PROFILE) {
    $historyCheck = Get-Content -Path $PROFILE | Select-String -Pattern "\$historyFile = `"$HOME\\PowerShellHistory.txt`""
    if ($historyCheck) {
        Write-Output "PowerShell history file already registered in $PROFILE"
        return 0
    }
} else {
    Write-Output "Creating new PowerShell profile at $PROFILE"
    New-Item -Path $PROFILE -ItemType File -Force
}

Write-Output "Registering PowerShell history file in $PROFILE"
Add-Content -Path $PROFILE -Value "\$historyFile = `"$HOME\\PowerShellHistory.txt`""
Add-Content -Path $PROFILE -Value "Register-EngineEvent PowerShell.Exiting -Action {"
Add-Content -Path $PROFILE -Value "    Get-History | Export-Clixml -Path \\$historyFile"
Add-Content -Path $PROFILE -Value "}"
Add-Content -Path $PROFILE -Value ""
Add-Content -Path $PROFILE -Value "if (Test-Path $historyFile) {"  
Add-Content -Path $PROFILE -Value "    \$history = Import-Clixml -Path \$historyFile"  
Add-Content -Path $PROFILE -Value "    \$history | ForEach-Object { Add-History -InputObject \$_ }"  
Add-Content -Path $PROFILE -Value "}"  
